package top.ljjapp.security.browser;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("登录用户名：" + username);
        //根据用户名查找用户信息,实际开发中从数据库中读出来
        //返回一个security已经实现的UserDetails的对象
        //第一个参数是用户名，第二个是密码，第三个是权限信息
        String password = passwordEncoder.encode("123456");
        log.info(password);
        return new User("ljj", password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
//        return new User(username, "123456",
//                true,
//                true,
//                true,
//                true,
//                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }

}
